//
//  WbStatusesViewModel.swift
//  Weibo
//
//  Created by HH on 16/12/5.
//  Copyright © 2016年 Weibo. All rights reserved.
//

import UIKit


class WbStatusesViewModel: NSObject {
    
    var statuses:WbStatusesModel?{
        
        didSet {
            
            guard let currentStatuses = statuses else {
                return
            }
            
            //处理来源
            handleSource(string: currentStatuses.source)
            //处理认证级别
            handleVerified_type(verified_type: (currentStatuses.user?.verified_type)!)
            //处理会员等级图片
            handleMbrankImage(mbrank: (currentStatuses.user?.mbrank)!)
            //处理三个按钮的显示内容
            retweetCountContent = handleCountContent(count: currentStatuses.reposts_count, defaultTitle: "转发")
            commentCountContent = handleCountContent(count: currentStatuses.comments_count, defaultTitle: "评论")
            praiseCountContent = handleCountContent(count: currentStatuses.attitudes_count, defaultTitle: "赞")
            //处理转发微博内容
            handleRetweetContent(statuses: statuses)
            
        }
    }
    
    //转发微博内容
    var retwetStatusesContent:String?
    //来源
    var sourceName:String?
    //认证图片
    var verified_typeImage:UIImage?
    //会员等级
    var mbrankImage:UIImage!
    //  转发数内容
    var retweetCountContent: String?
    //  评论数内容
    var commentCountContent: String?
    //  赞数内容
    var praiseCountContent: String?
    //配图size
    var pictureViewItemSize:CGSize = CGSize(width: 0, height: 0)
    //时间
    var statusesTime:String{
        
        return (statuses?.created_at?.sinaTimeString)!
    }
    
    
    //处理来源
    private func handleSource(string:String?){
        
        guard let source = string else {
            return
        }
        
        if ((source as NSString).length == 0){
        
        return
        }
        
        let firstSubArr = source.components(separatedBy: ">")
        
        let secondSubArr = firstSubArr[1].components(separatedBy: "<")
        
        sourceName = secondSubArr[0]
        
    }
    
    //处理认证图片
    // 认证类型 -1：没有认证，1，认证用户，2,3,5: 企业认证，220: 达人
    private func handleVerified_type(verified_type:Int){
        
        switch verified_type {
        case 0:
            verified_typeImage = #imageLiteral(resourceName: "avatar_vip_golden")
        case 2,3:
            verified_typeImage = #imageLiteral(resourceName: "avatar_enterprise_vip")
        case 220:
            verified_typeImage = #imageLiteral(resourceName: "avatar_grassroot")
        default:
            verified_typeImage = nil
        }
        
    }
    
    //处理会员图片
    private func handleMbrankImage(mbrank:Int){
        
        if mbrank >= 0 && mbrank <= 6 {
            
            mbrankImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        
    }
    
    //处理显示数量
    private func handleCountContent(count:Int,defaultTitle:String)->(String){
        
        if count > 10000 {
            let handelCount = CGFloat(count / 1000) / 10
            
            let str = "\(handelCount)万"
            
            if str.contains(".0") {
                //  替换特定字符串
                return str.replacingOccurrences(of: ".0", with: "")
            } else {
                return str
            }
        }else if count > 0{
            
            return "\(count)"
        }else{
            
            return defaultTitle
        }
        
    }
    
    private func handleRetweetContent(statuses:WbStatusesModel?){
        
        if statuses?.retweeted_status != nil{
            
            if let name = statuses?.retweeted_status?.user?.screen_name,let text = statuses?.retweeted_status?.text {
                
                retwetStatusesContent = "@" + "\(name):\(text)"
            }
        }
        
        
    }
    
    

}
